package com.hopu.oa.controller.management;

import com.hopu.oa.dao.management.AoaAttendsListDao;
import com.hopu.oa.entity.management.AoaAttendsList;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.management.AoaAttendsListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Controller
public class AttendanceController {
    @Autowired
    private AoaAttendsListService aoaAttendsListService;
    @Autowired
    private AoaAttendsListDao aoaAttendsListDao;


    @RequestMapping("/singin")
    public String singin(Model model, HttpSession session, HttpServletRequest request) {

        Integer userId = ((AoaUser) session.getAttribute("user")).getUserId();
        //获取ip
        String remoteAddr = request.getRemoteAddr();
        //定义上班时间
        String begin = "09:00", end = "17:00";
        //定义上班签到时间

        Date date = new Date();
        String format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        String format2 = new SimpleDateFormat("yyyy-MM-dd").format(date);
        String format3 = new SimpleDateFormat("HH:mm").format(date);
        String eeee = new SimpleDateFormat("EEEE").format(date);//星期
        AoaAttendsList aoaAttendsList = new AoaAttendsList();

        if (format3.compareTo("08:00") < 0) {
            System.out.println("还未到打卡时间");
            model.addAttribute("error", '2');
        } else if (format3.compareTo("23:00") > 0) {
            System.out.println("不在正常签到时间内");
            model.addAttribute("error", '1');
        } else {
            List<AoaAttendsList> aoaAttendsLists = aoaAttendsListService.selectByUserIDAndTime(format2, userId);
            if (aoaAttendsLists.size() == 0) {
                aoaAttendsList.setAttendsIp(remoteAddr);
                aoaAttendsList.setAttendsTime(format1);
                if (format3.compareTo(begin) > 0) {
                    aoaAttendsList.setStatusId(11);//迟到
                } else {
                    aoaAttendsList.setStatusId(10);//正常
                }
                aoaAttendsList.setTypeId(8);//上班
                aoaAttendsList.setAttendsUserId(userId);
                aoaAttendsList.setAttendHmtime(format3);
                aoaAttendsList.setWeekOfday(eeee);
                aoaAttendsListDao.insert(aoaAttendsList);
                model.addAttribute("type", "上班");
                model.addAttribute("aalist", aoaAttendsList);
            } else if (aoaAttendsLists.size() == 1) {
                aoaAttendsList.setAttendsIp(remoteAddr);
                aoaAttendsList.setAttendsTime(format1);
                if (format3.compareTo(end) > 0) {
                    aoaAttendsList.setStatusId(10);//正常
                } else {
                    aoaAttendsList.setStatusId(12);//早退
                }
                aoaAttendsList.setTypeId(9);//下班
                aoaAttendsList.setAttendsUserId(userId);
                aoaAttendsList.setAttendHmtime(format3);
                aoaAttendsList.setWeekOfday(eeee);
                aoaAttendsListDao.insert(aoaAttendsList);
                model.addAttribute("type", "下班");
                model.addAttribute("aalist", aoaAttendsList);
            } else {
                Integer attendsUserId = aoaAttendsLists.get(1).getAttendsUserId();
                Integer attendsId = aoaAttendsLists.get(1).getAttendsId();
                aoaAttendsList.setAttendsId(attendsId);
                aoaAttendsList.setAttendsIp(remoteAddr);
                aoaAttendsList.setAttendsTime(format1);
                if (format3.compareTo(end) > 0) {
                    aoaAttendsList.setStatusId(10);
                } else {
                    aoaAttendsList.setStatusId(12);
                }
                aoaAttendsList.setTypeId(9);
                aoaAttendsList.setAttendsUserId(attendsUserId);
                aoaAttendsList.setAttendHmtime(format3);
                aoaAttendsList.setWeekOfday(eeee);
                aoaAttendsListDao.updateById(aoaAttendsList);
                model.addAttribute("type", "下班");
                model.addAttribute("aalist", aoaAttendsList);
            }
        }
        return "systemcontrol/signin";
    }


    @RequestMapping("/attendcelist")
    public String selectAll(String baseKey, Model model, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "4") int size, HttpSession session) {
        Integer userId = ((AoaUser) session.getAttribute("user")).getUserId();
        //总数据
        int totalElements = aoaAttendsListService.selectAll(userId, baseKey, page, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaAttendsList> list = aoaAttendsListService.selectAll(userId, baseKey, (page - 1) * size, size);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("alist", list);
        model.addAttribute("url", "selectDis");
        return "attendce/attendcelist";
    }

    @RequestMapping("/selectDis")
    public String selectDis(String baseKey, Model model, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "4") int size, HttpSession session) {
        Integer userId = ((AoaUser) session.getAttribute("user")).getUserId();
        //总数据
        int totalElements = aoaAttendsListService.selectAll(userId, baseKey, page, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaAttendsList> list = aoaAttendsListService.selectAll(userId, baseKey, (page - 1) * size, size);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);

        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("alist", list);
        model.addAttribute("url", "selectDis");
        return "attendce/attendcelisttable";
    }


}
